package com.example.shopdemo.controller.shop;

import com.example.shopdemo.dto.ProductCategoryExecution;
import com.example.shopdemo.dto.Result;
import com.example.shopdemo.entity.ProductCategory;
import com.example.shopdemo.entity.Shop;
import com.example.shopdemo.enums.ProductCategoryStateEnum;
import com.example.shopdemo.service.ProductCategoryServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-01-21
 **/
@Controller
@RequestMapping("/shop")
public class ProductCategoryManagementController {

    @Autowired
    ProductCategoryServiceImpl productCategoryService;

    @RequestMapping("/getProductCategoryList")
    @ResponseBody
    public Result<List<ProductCategory>> getProductCategoryList(HttpServletRequest request){
        Shop currentShop =(Shop)request.getSession().getAttribute("currentShop");
        if(currentShop!=null&&currentShop.getShopId()>0){
            List<ProductCategory> productCategoryList = productCategoryService.getByShopId(currentShop.getShopId());
            return new Result<List<ProductCategory>>(true,productCategoryList);
        }else{
            return new Result(false, ProductCategoryStateEnum.INNER_ERROR.getKey(),ProductCategoryStateEnum.INNER_ERROR.getValue());
        }
    }

    @RequestMapping(value = "/addProductCategoryList",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addProductCategoryList(@RequestBody List<ProductCategory> productCategoryList,HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();
        Shop currentShop = (Shop)request.getSession().getAttribute("currentShop");
        for(ProductCategory pc:productCategoryList){
            pc.setShopId(currentShop.getShopId());
            pc.setCreateTime(new Date());
        }
        if(productCategoryList!=null&&productCategoryList.size()>0){
            ProductCategoryExecution productCategoryExecution = productCategoryService.batchAddProductCategory(productCategoryList);
            if(productCategoryExecution.getState()==ProductCategoryStateEnum.SUCCESS.getKey()){
                modelMap.put("success",true);
            }else{
                modelMap.put("success",false);
                modelMap.put("errMsg",productCategoryExecution.getStateInfo());
            }
        }
        return modelMap;
    }

    @RequestMapping(value = "/removeProductCategory",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> removeProductCategory(@RequestParam("productCategoryId") Long productCategoryId,HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();
        if(productCategoryId!=null&&productCategoryId>0){
            Shop currentShop = (Shop)request.getSession().getAttribute("currentShop");
            ProductCategoryExecution productCategoryExecution = productCategoryService.deleteProductCategory(productCategoryId,currentShop.getShopId());
            if(productCategoryExecution.getState()==ProductCategoryStateEnum.SUCCESS.getKey()){
                modelMap.put("success",true);
            }else{
                modelMap.put("success",false);
                modelMap.put("errMsg",productCategoryExecution.getStateInfo());
            }
        } else{
            modelMap.put("success",false);
            modelMap.put("errMsg","请至少选择一个商品类别");
        }
        return modelMap;
    }


}
